package cn.com.coding.system.service.impl;

import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.common.utils.Query;
import cn.com.coding.system.dao.UserCardProductDao;
import cn.com.coding.system.domin.ProductImageEntity;
import cn.com.coding.system.domin.UserCardEntity;
import cn.com.coding.system.domin.UserCardProductEntity;
import cn.com.coding.system.domin.dto.UserCardProductDto;
import cn.com.coding.system.service.ProductImageService;
import cn.com.coding.system.service.UserCardProductService;
import cn.com.coding.system.service.UserCardService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("userCardGoodService")
@Transactional(rollbackFor = Exception.class)
public class UserCardProductServiceImpl extends ServiceImpl<UserCardProductDao, UserCardProductEntity> implements UserCardProductService {

    @Resource
    private UserCardService userCardService;

    @Resource
    private ProductImageService productImageService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<UserCardProductEntity> page = this.page(new Query<UserCardProductEntity>().getPage(params), new QueryWrapper<UserCardProductEntity>());

        return new PageUtil(page);
    }

    @Override
    public List<UserCardProductDto> getCardProductByUserPid(String userPid) {
        List<UserCardProductDto> list = this.baseMapper.getCardProductByUserPid(userPid);
        list.forEach(e -> {
            List<ProductImageEntity> imgList = productImageService.list(new QueryWrapper<ProductImageEntity>().eq("product_pid", e.getProductPid()));
            e.setImgUrl(imgList);
        });
        return list;
    }

    @Override
    public List<UserCardProductEntity> getCardProductByCardPid(String cardPid) {
        return this.baseMapper.getCardProductByCardPid(cardPid);
    }

    @Override
    public void deleteProduct(String userPid, String productPid) {

        UserCardEntity card = userCardService.getOne(new QueryWrapper<UserCardEntity>().eq("user_pid", userPid).eq("card_status", 0));

        this.baseMapper.deleteById(productPid);

    }

    @Override
    public List<UserCardProductDto> getOrderList(String cardPid) {
        List<UserCardProductDto> list = this.baseMapper.getOrderList(cardPid);
        list.forEach(e -> {
            List<ProductImageEntity> imgList = productImageService.list(
                    new QueryWrapper<ProductImageEntity>().eq("product_pid", e.getProductPid()));
            e.setImgUrl(imgList);
        });
        return list;
    }

}